package com.elvin.wx.controller;


import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class SynchronizedByKey {
    Map<String,Object> mutextCache = new ConcurrentHashMap<>();
    public void exec(String key,Runnable statement){
        Object mutext4key = mutextCache.computeIfAbsent(key, k -> new Object());
        synchronized (mutext4key){
            try {
                statement.run();
            } finally {
                mutextCache.remove(mutext4key);
            }
        }
    }
}
